

module.exports = class extends baseController{
    constructor(){
        super();
        this.systemModuleLogic=this.loadLogic('iframe/system-module');
        this.router.post('/getItems',this.getItems.bind(this));
        this.router.post('/editModule',this.editModule.bind(this));
        this.router.post('/delModule',this.delModule.bind(this));
        this.router.post('/moveNode',this.moveNode.bind(this));
        this.router.post('/publish',this.publish.bind(this));
        this.router.post('/getJsonFile',this.getJsonFile.bind(this));
        this.router.post('/rollBack',this.rollBack.bind(this));
        return this.router;
    }

    static init(){
        return new this();
    }

    async getItems(req,res){
        let Result={};
        try{
            let ret=await this.systemModuleLogic.getItems();
            Result=this.Success(1,ret);
        }catch(err){
            Result=this.Error('数据查询失败',err);
        }
        res.json(Result);
    }

    async editModule(req,res){
        let Result={};
        try {
            let ret=await this.systemModuleLogic.insertOrUpdate(req.body);
            Result=this.Success(1,ret);
        }
        catch(err){
            Result=this.Error('操作失败',err);
        }
        res.json(Result);
    }

    async delModule(req,res){
        let Result={};
        try {
            let AllIDs = req.body.AllIDs;
            let CurNodeParentID = req.body["CurNodeParentID"];
            let AllIDs_Array = AllIDs.split(',');
            let ret=await this.systemModuleLogic.delModule(AllIDs_Array,CurNodeParentID);
            Result=this.Success(1,ret);
        }
        catch(err){
            Result=this.Error('操作失败',err);
        }
        res.json(Result);
    }

    async moveNode(req,res){
        let Result={};
        try {
            let queryModel={
                CurNodeID : Number("0" + req.body["CurNodeID"]),
                TargetParentID : Number("0" + req.body["TargetParentID"]),
                old_parent : Number("0" + req.body["old_parent"]),
                TargetNodeChildrensIDs : req.body["TargetNodeChildrensIDs"] + ",",

            };
            let ret=await this.systemModuleLogic.moveNode(queryModel);
            Result=this.Success(1,ret);
        }
        catch(err){
            Result=this.Error('操作失败',err);
        }
        res.json(Result);
    }

    async publish(req,res){
        let Result={};
        try {
            let ret=await this.systemModuleLogic.publish();
            Result=this.Success(1,ret);
        }
        catch(err){
            Result=this.Error('操作失败',err);
        }
        res.json(Result);
    }

    async getJsonFile(req,res){
        let Result={};
        try {
            let ret=await this.systemModuleLogic.getJsonFile();
            Result=this.Success(1,ret);
        }
        catch(err){
            Result=this.Error('操作失败',err);
        }
        res.json(Result);
    }

    async rollBack(req,res){
        let Result={};
        try {
            let  FileName = req.body.FileName || '';
            if(FileName===''){
                return res.json(this.Error('请选择需要恢复的文件'));
            }
            let ret=await this.systemModuleLogic.rollBack(FileName);
            Result=this.Success(1,ret);
        }
        catch(err){
            Result=this.Error('操作失败',err);
        }
        res.json(Result);
    }
};